
package org.unisannio.sc;



import it.unipr.ce.dsg.s2p.sip.Address;

import java.util.ArrayList;
import java.util.List;

import org.unisannio.remote.FactoryRemote;
import org.unisannio.remote.RemoteContentProvider;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;


public class AddressArrayAdapter extends ArrayAdapter<Address> {
	
	public Activity context;
	List<Address> items;
	int listaDaCreare;
	ArrayList<Address> address;
	
	public AddressArrayAdapter(Activity context, int layout, List<Address> items){
		super(context, layout, items);
		this.context=context;
		this.items=items;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row=convertView;
		final int position2=position;
		if(row==null){
			LayoutInflater inflater=context.getLayoutInflater();
			row=inflater.inflate(R.layout.addresses, parent, false);
		}
		
		ImageButton button=(ImageButton) row.findViewById(R.id.button1);
		button.setImageResource(android.R.drawable.ic_menu_search);
		TextView label = (TextView) row.findViewById(R.id.textViewAddress);
		label.setText(items.get(position).toString());
		button.setBackgroundColor(Color.TRANSPARENT);
		button.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				address=new ArrayList<Address>();
				address.add( items.get(position2));
				get();
			}
		});
		

		return row;
	}

	private void get() {
		// TODO Auto-generated method stub
		LayoutInflater inflater=LayoutInflater.from(context);
		View addView = inflater.inflate(R.layout.get, null);
		final DialogWrapper wrapper = new DialogWrapper(addView);
		new AlertDialog.Builder(context)
				.setTitle("Search")
				.setView(addView)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						processGet(wrapper);
					}

					

				})
		.setNegativeButton("Cancel",
												new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog,
														int whichButton) {
				// ignore, just dismiss
			}
		})
		.show();
	}
	private void processGet(DialogWrapper wrapper) {
		String display_name=wrapper.getNome().trim()+" "+wrapper.getCognome().trim();
		display_name=display_name.trim();

		
		
		RemoteContentProvider contentRemote=  new FactoryRemote().getRemoteContentProvider(context,3000);
		contentRemote.saveChosenAddress(address);
		new QueryContactsTask(context, display_name).execute(contentRemote);
		
		
	}
	
	class DialogWrapper {
		EditText nome=null;
		EditText cognome=null;
		
		View base=null;
		
		DialogWrapper(View base) {
			this.base=base;
			
		}
		
		String getNome() {
			return(getNomeField().getText().toString());
		}
		
		String getCognome() {
			return((getCognomeField().getText().toString()));
		}
		
		
		private EditText getCognomeField() {
			if (cognome==null) {
				cognome=(EditText)base.findViewById(R.id.cognome);
			}
			
			return(cognome);
		
		}

		private EditText getNomeField() {
			if (nome==null) {
				nome=(EditText)base.findViewById(R.id.nome);
			}
			
			return(nome);
		}
		
		
	}
}
